import Vue from 'vue';
import VueRouter from 'vue-router';
import Home from '../views/Home.vue';
// import Home_page from '../views/Home_page.vue'
// import Home_page from '../views/Home_page.vue'
import About from '../views/About.vue';


Vue.use(VueRouter);

const routes = [
  {
    path: '/',
    name: 'home',
    component: Home,
    // path: '/about',
    //  component: About,

    //      name: 'about',

  },
  {
    path: '/about',
    name: 'about',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
   component:About,
  },
  {
    path: '/register',
    name: 'register',
    component: () => import('../components/Register.vue'),
  },
  {
    path: '/index',
    name: 'index',
    component: () => import('../views/Index.vue'),
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('../components/Login.vue'),
  },
  {
    path: '/forgetPwd',
    name: 'forgetPwd',
    component: () => import('../views/ForgetPwd.vue'),
  },
  {
    path: '/homePage',
    name: 'homePage',
    component: () => import('../views/HomePage.vue'),
  },
  {
    path: '/store',
    name: 'store',
    component: () => import('../views/StoreIndex.vue'),
  },
  {
    path: '/store/goods',
    name: 'storeGoods',
    component: () => import('../views/StoreGoods.vue'),
  },
  {
    path: '/person',
    name: 'person',
    component: () => import('../views/PersonIndex.vue'),
  },
  // {
  //   path: '/person/pwd',
  //   name: 'password',
  //   component: () => import('../views/Password.vue'),
  // },
  {
    path: '/goods',
    name: 'goods',
    component: () => import('../views/GoodsIndex.vue'),
  },
  // {
  //   path: '/changeAll',
  //   name: 'changeAll',
  //   component: () => import('../views/ChangeAll.vue'),
  // },
  // {
  //   path: '/addStore',
  //   name: 'addStore',
  //   component: () => import('../views/addGoods.vue'),
  // },
  // {
  //   path: '/indexStu',
  //   name: 'indexStu',
  //   meta: { requireAuth: true },
  //   component: () => import(/* webpackChunkName: "about" */ '../views/students/Index_stu.vue'),
  // },

];

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes,
});

router.beforeEach((to, from, next) => {
  console.log(`router to: ${to.path}, from: ${from.path}`)
  if (sessionStorage.getItem('Login') === 'true' && to.path === '/') {
    next(false)
  }
  if (to.matched.some(record => record.meta.requireAuth)) { // 判断该路由是否需要登录权限
    if (sessionStorage.getItem('Login') === 'true') {
      next();
    } else {
      next({
        path: '/',
        query: { redirect: to.fullPath }, // 将跳转的路由path作为参数，登录成功后跳转到该路由
      });
    }
  } else {
    next();
  }
});

export default router;
